package com.letwal.cmcvnd.earn.withdraw

import android.annotation.SuppressLint
import android.view.View
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.letwal.cmcvnd.R
import com.letwal.cmcvnd.adata.income.ReqExtractInviteIncome
import com.letwal.cmcvnd.adata.income.ReqGetInviteIncomeData
import com.letwal.cmcvnd.aviewmodel.IncomeViewModel
import com.letwal.cmcvnd.base.BaseActivity
import com.letwal.cmcvnd.components.dialog.EnterTransferPasswordDialog
import com.letwal.cmcvnd.databinding.ActivityWithdrawForInviteEarnBinding
import com.letwal.cmcvnd.enums.PageTagEnum
import com.letwal.cmcvnd.extend.*
import com.letwal.cmcvnd.home.repo.HomeCoinManager
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.HashMap

/** 我的币页 */
@SuppressLint("CustomSplashScreen")
class WithdrawForInviteEarnActivity : BaseActivity() {

    //布局binding
    private lateinit var mActivityBinding: ActivityWithdrawForInviteEarnBinding

    //密码弹框
    private lateinit var mEnterTransferPasswordDialog: EnterTransferPasswordDialog

    //income viewModel
    private val mIncomeViewModel by lazy {
        ViewModelProvider(this)[IncomeViewModel::class.java]
    }

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mIncomeViewModel)
        return list
    }

    override fun onBindingViewTag(): String {
        return PageTagEnum.my_wallet.name.uppercase()
    }

    override fun onBindingContentView(): View {
        mActivityBinding =
            ActivityWithdrawForInviteEarnBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    override fun onContentViewBinded() {
        try {
            //监听返回
            onHandleLiveDataCallback()

            //初始化
            mEnterTransferPasswordDialog = EnterTransferPasswordDialog()
            mEnterTransferPasswordDialog.setConfirmClickListener { password ->
                //确认提现
                val amount = mActivityBinding.editWithdrawEnterAmount.text.toString().trim()
                HomeCoinManager.mGlobalCoinTypeLiveData.value?.let {
                    mIncomeViewModel.extractInviteIncome(
                        ReqExtractInviteIncome(
                            coinType = it,
                            password = password,
                            amount = amount
                        )
                    )
                }
            }

            mActivityBinding.submitWithdrawEnterAmount.setOnClickListener {
                if (mIncomeViewModel.canWithdrawAmount.toAmountDouble() > 2) {
                    val amount = mActivityBinding.editWithdrawEnterAmount.text.toString().trim()
                    if (amount.toAmountDouble() > 2) {
                        mEnterTransferPasswordDialog.showDialog(
                            supportFragmentManager,
                            getString(R.string.withdraw_enter_your_password)
                        )
                    } else {
                        Toast.makeText(
                            this,
                            getString(R.string.withdraw_err_tips1),
                            Toast.LENGTH_SHORT
                        )
                            .show()
                    }
                } else {
                    Toast.makeText(this, getString(R.string.withdraw_err_tips2), Toast.LENGTH_SHORT).show()
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onResume() {
        super.onResume()

        HomeCoinManager.mGlobalCoinTypeLiveData.value?.let {
            //获取转入收益详情
            mIncomeViewModel.getInviteIncomeData(ReqGetInviteIncomeData(coinType = it))
        }
    }

    /** 监听live data 返回 */
    @SuppressLint("NotifyDataSetChanged", "SetTextI18n", "SimpleDateFormat")
    private fun onHandleLiveDataCallback() {
        mIncomeViewModel.onGetInviteIncomeDataLiveData.observe(this) {
            if (it.isSuccess) {
                it.getOrNull()?.let { result ->
                    mIncomeViewModel.canWithdrawAmount = result.canWithdrawAmount
                    mActivityBinding.textWithdrawAmountThatCanBeWithdrawnValue.text =
                        result.canWithdrawAmount.toFinanceString() + " USDT"
                }
            }
        }

        mIncomeViewModel.onExtractInviteIncomeLiveData.observe(this) {
            if (it.isSuccess) {

                val dateFormat = SimpleDateFormat("dd/MM/yyyy")
                val date = dateFormat.format(Date())

                val address = HomeCoinManager.mGlobalAddressLiveData.value ?: ""
                val amount = mActivityBinding.editWithdrawEnterAmount.text.toString().trim()
                val params = HashMap<String, String>()
                params["amount"] = amount
                params["address"] = address
                params["date"] = date
                params["unit"] = "USDT"

                startToEarnWithdrawTransferResultActivity(hashMap = params)
            }
        }
    }
}